package scedu.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import com.scedu.springcloud.entity.Dept;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import scedu.service.DeptService;

import java.util.List;

//提供restful服务
@RestController
@Slf4j
public class DeptController {
    @Autowired
    DeptService service;
    /*添加

     */
    @PostMapping("/add")
    public Integer add(Dept dept){
        return service.addDept(dept);
    }
    /*查询单个

     */
    @GetMapping("/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Dept get( @PathVariable("id") Long id){
        log.info("id={}",id);
        Dept dept = service.queryById(id);
        if (dept==null){
            throw  new RuntimeException("=>"+id+"不存在该客户信息。");
        }
        return dept;
    }
    /*
    hystrix备选查询单个
     */
    public Dept hystrixGet( @PathVariable("id") Long id){
        Dept dept=new Dept()
                .setDeptno(id)
                .setDname("不存在该客户信息。")
                .setDbSource("no datebase");

        return dept;
    }

    /*查询所有

     */
    @GetMapping("/all")
    public List<Dept> queryALL(){
        return service.queryALL();
    }


}
